package com.binarysoft.cmdhandler.product;

import java.util.Collection;
import java.util.StringTokenizer;

import com.binarysoft.service.product.Product;
import com.binarysoft.service.product.ProductService;
import common.framework.command.handler.AbstractCommandHandler;
import common.framework.command.helper.CommandHelper;
import common.framework.command.server.Terminal;
import common.framework.dsb.annotation.DSBCmd;
import common.framework.dsb.client.DefaultServiceFactory;
import common.framework.dsb.client.ServiceFactory;

@DSBCmd("product")
public class ProductCmdHandler extends AbstractCommandHandler {

	@Override
	public void handle(String args, Terminal terminal) throws Exception {
		terminal.println("");
		StringTokenizer stier = new StringTokenizer(args);
		int count = stier.countTokens();
		if (count < 2) {
			showOptions(terminal);
			return;
		}
		stier.nextToken(); // product
		String cmd = stier.nextToken();
		if ("ls".equalsIgnoreCase(cmd)) {
			listProducts(terminal);
		} else if ("add".equalsIgnoreCase(cmd)) {
			addProduct(terminal);
		} else if ("del".equalsIgnoreCase(cmd) && count == 3) {
			String productID = stier.nextToken();
			deleteProduct(terminal, Long.parseLong(productID));
		} else if ("update".equalsIgnoreCase(cmd) && count == 3) {
			String productID = stier.nextToken();

			ServiceFactory serviceFactory = new DefaultServiceFactory();
			ProductService productService = serviceFactory.lookup("DSB/ProductService");
			Product prod = productService.getProduct(Long.parseLong(productID));

			if (prod == null) {
				terminal.print("Product not found!");
				return;
			}
			terminal.print("Please input product name:");
			String name = terminal.read();
			prod.setName(name);
			productService.updateProduct(prod);

			terminal.println(" product with name:" + name + " updated.");

		} else {
			showOptions(terminal);
		}
		terminal.println("");
	}

	private void deleteProduct(Terminal terminal, long productID) throws Exception {
		ServiceFactory serviceFactory = new DefaultServiceFactory();
		ProductService productService = serviceFactory.lookup("DSB/ProductService");
		Product prod = productService.getProduct(productID);
		if (prod == null) {
			terminal.print("Product not found!");
			return;
		}
		productService.deleteProduct(productID);
		terminal.print("Product with name:" + prod.getName() + " deleted successful!");
	}

	private void addProduct(Terminal terminal) throws Exception {
		terminal.print("Please input product name:");
		String productName = terminal.read();
		terminal.print("Please input product type:");
		String productType = terminal.read();
		terminal.print("Please input product desc:");
		String productDesc = terminal.read();
		Product product = new Product();
		product.setName(productName);
		product.setType(productType);
		product.setDescription(productDesc);
		ServiceFactory serviceFactory = new DefaultServiceFactory();
		ProductService productService = serviceFactory.lookup("DSB/ProductService");
		productService.addProduct(product);
		terminal.print("Product with name:" + productName + " added successful!");
	}

	private void listProducts(Terminal terminal) throws Exception {
		ServiceFactory serviceFactory = new DefaultServiceFactory();
		ProductService productService = serviceFactory.lookup("DSB/ProductService");
		Collection<Product> products = productService.getAllProducts();

		String[] tableHAD = { "Product ID", "Product Name", "Type", "Creation Time" };
		String[] tableGAP = { "----------", "------------", "----", "-------------" };
		int[] format = { 15, 25, 15, 30 };
		terminal.println(CommandHelper.format(tableHAD, format));
		terminal.println(CommandHelper.format(tableGAP, format));

		for (Product prd : products) {
			Object[] row = { prd.getId(), prd.getName(), prd.getType(), CommandHelper.getTime(prd.getCreationTime().getTime()) };
			terminal.println(CommandHelper.format(row, format));
		}

	}

	private void showOptions(Terminal terminal) throws Exception {
		terminal.println(" product ls                   --list all products");
		terminal.println(" product add                  --add new product");
		terminal.println(" product del    <product ID>  --delete product");
		terminal.println(" product update <product ID>  --update product");
		terminal.println("");
	}
}
